package com.bml.architect.netty.serialiable;

import com.bml.architect.entity.MsgProto;
import com.bml.architect.entity.User;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;


public class SerialClientHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("接受到服务端发送的消息：" +msg);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("开始向服务端发送数据");
        for(int i = 1;i<20;i++){
            User user = new User();
            user.setId(i);
            user.setName("bml" + i);
            user.setAddress("四川成都市");
            // ctx.writeAndFlush(user);
            MsgProto msg = new MsgProto();
            msg.setContent(user.toString().getBytes("UTF-8"));
            msg.setLength(user.toString().getBytes("UTF-8").length);
            ctx.writeAndFlush(msg);
        }

    }
}
